// 连接数据库，导出db对象
const mongoose = require('mongoose')
const db = mongoose.createConnection("mongodb://localhost:27017/blogproject", {
  useNewUrlParser: true
})

// 用原生Promise代替mongoose自带的Promise
mongoose.Promise = global.Promise

// 取出mongoose的Schema
const Schema = mongoose.Schema

// 监听数据库连接
db.on("error", () => {
  console.log('数据库连接失败');
})
db.on("open", () => {
  console.log('数据库连接成功');
})

// 导出db对象
module.exports = {
  db,
  Schema
}